<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Event;

/**
 * 广告
 * @mixin \think\Model
 */
class Ad extends Base
{
    protected $name = 'ad';

    public static function _save($param): array
    {
        try {
            if($param['link_type'] == 2){
                $arr = [];
                !empty($param['cate_1']) && $arr[] = $param['cate_1'];
                !empty($param['cate_2']) && $arr[] = $param['cate_2'];
                !empty($param['cate_3']) && $arr[] = $param['cate_3'];
                $param['link_id'] = implode(',', $arr);
            }elseif($param['link_type'] == 3){
                $param['link_id'] = $param['product_id'];
            }else{
                $param['link_id'] = 0;
            }

            if (isset($param['id']) && $param['id']) {
                $model = parent::update($param);
                $model->operation = 'save';
            } else {
                $model = parent::create($param);
                $model->operation = 'create';
            }

            Event::trigger('Logs', $model);

            return ['code' => 1, 'msg' => '操作成功'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

}
